package com.codeoss.framework.workflow.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.codeoss.framework.workflow.def.WorkflowDef;
import com.codeoss.framework.workflow.parser.IWorkflowElementParser;
import com.codeoss.module.workflow.model.WorkflowWorkflow;
import com.codeoss.module.workflow.service.IWorkflowWorkflowService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Component
public class WorkflowUtil {
	@Autowired
	private IWorkflowWorkflowService workflowWorkflowService;
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	@Qualifier("workflowParser")
	private IWorkflowElementParser workflowElementParser;
	public WorkflowDef getWorkflowDefByWorkflowId(Long workflowWorkflowId) {
		WorkflowWorkflow workflowWorkflow=workflowWorkflowService.getById(workflowWorkflowId);
		String json=workflowWorkflow.getJsonContent();
		try {
			ObjectNode modelNode=(ObjectNode) objectMapper.readTree(json);
			WorkflowDef workflowDef=(WorkflowDef)workflowElementParser.parse(null, modelNode);
			return workflowDef;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
}
